
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Gui;
using Swaf.Security;
namespace Approval.Gui
{
	public class ApprovalActionBizProc : BaseBusinessProcess
	{
		public ApprovalActionBizProc(ActiveBizProc procState): base(procState){}

		public override void updateActionState()
		{
			IBizObj thebizobj = CurrentBizObj;
			bool isApprover = hasApprovalPriv();
			((GenericAction)Actions["GenerateFromYouTube"]).Availability = CanEdit ? ActionAvailability.enabled : ActionAvailability.none;

			((GenericAction)Actions["Approve"]).Availability = isApprover && !HasVoted && "0".Equals(thebizobj.get("state").ToString())
				? ActionAvailability.enabled : ActionAvailability.none;
			((GenericAction)Actions["Denied"]).Availability = isApprover && canDeny()
				? ActionAvailability.enabled : ActionAvailability.none;
			((GenericAction)Actions["RollBack"]).Availability = isApprover ? ActionAvailability.enabled : ActionAvailability.none;
		}

		protected override object[] getDetailsBizRuleParameters(object keyValue)
		{
			return base.getDetailsBizRuleParameters(CurrentBizObj);
		}

		//TODO: Fix this: This was added to get the stuff compiling, but this code needs serious, serious work.
		void OnBizProcException(object a, string b, object c)
		{
		}

		protected void executeApprove()
		{
			
			IResults rc = App.ruleMgr.execute("executeApprovalAction", CurrentBizObj, ApprovalActionState.Approve);
			if (rc.ExceptionInfo != null)
				OnBizProcException(this, "executeApprove", rc.ExceptionInfo);
			
		}
		protected void executeDenied()
		{
			IResults rc = App.ruleMgr.execute("executeApprovalAction", CurrentBizObj, ApprovalActionState.Denied);
			if (rc.ExceptionInfo != null)
				OnBizProcException(this, "executeApprove", rc.ExceptionInfo);
		}

		protected void executeRollBack()
		{
			IResults rc = App.ruleMgr.execute("executeApprovalAction", CurrentBizObj, ApprovalActionState.Rollback);
			if (rc.ExceptionInfo != null)
				OnBizProcException(this, "executeApprove", rc.ExceptionInfo);
		}

		protected override void setupActions()
		{
			base.setupActions();
			Actions.addAction(new GenericAction("Approve", false,executeApprove, this));
			Actions.addAction(new GenericAction("Denied", false, executeDenied, this));
			Actions.addAction(new GenericAction("RollBack", false, executeRollBack, this));
		}

		

		protected bool canDeny() 
		{
			IBizObj thebizobj = CurrentBizObj;
			if (thebizobj != null && thebizobj.containsField("state"))
			{
				if ("0".Equals(thebizobj.get("state").ToString()))
					return true;
				IResults rc = App.ruleMgr.execute("getNextApprover", thebizobj.get("approvalSet") as IBizObj);
				if (rc.ExceptionInfo != null)
					OnBizProcException(this, "updateActionState call to canDeny.", rc.ExceptionInfo);
				return rc.CallResults != null;
			} 
			return false;
		}
		protected bool canRollBack() { return true; }
		
		protected bool hasApprovalPriv()
		{
			IBizObj thebizobj = CurrentBizObj;
			if (thebizobj != null)
			{
				IResults rc = App.ruleMgr.execute("getApprovers", thebizobj.get("approvalSet") as IBizObj);
				if (rc.ExceptionInfo != null)
					OnBizProcException(this, "error executing getApprovers bizRule from hasApprovalPriv()", rc.ExceptionInfo);
				IBizObjList list = rc.CallResults as IBizObjList;
				IBizObj usr = list.search("id", CurrentUser.get("id") as Swaf.BGuid.Guid);
				return usr != null;
			}
			return false;
		}

		protected bool HasVoted
		{
			get
			{
				IBizObj thebizobj = CurrentBizObj;
				IResults rc = App.ruleMgr.execute("hasVoted", thebizobj.get("id"));
				if (rc.ExceptionInfo != null)
					OnBizProcException(this, "error executing hasVoted.", rc.ExceptionInfo);
				return (bool)rc.CallResults ;
			}
		}


		public override Privileges CurrentEditPrivilege
		{
			//get { return CurrentPrincipal.getPrivilege("AppFunctions.Approval"); }
			get { return Privileges.Add; }
		}

		protected override void setupRuleNames()
		{
			m_getBizObjDetailsRule = "getApprovalDetails";
		}
	}
}
